package top.gbiot.project.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.gbiot.common.annotation.Log;
import top.gbiot.common.core.controller.BaseController;
import top.gbiot.common.core.domain.AjaxResult;
import top.gbiot.common.core.domain.entity.SysUser;
import top.gbiot.common.core.page.TableDataInfo;
import top.gbiot.common.enums.BusinessType;
import top.gbiot.common.utils.ShiroUtils;
import top.gbiot.common.utils.poi.ExcelUtil;
import top.gbiot.project.domain.IotProject;
import top.gbiot.project.service.IIotProjectService;

/**
 * 项目管理Controller
 * 
 * @author gb
 * @date 2021-02-09
 */
@Controller
@RequestMapping("/iot/project")
public class IotProjectController extends BaseController
{
    private String prefix = "iot/project";

    @Autowired
    private IIotProjectService iotProjectService;

    @RequiresPermissions("iot:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目管理列表
     */
    @RequiresPermissions("iot:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IotProject iotProject)
    {
        startPage();
        List<IotProject> list = iotProjectService.selectIotProjectList(iotProject);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @RequiresPermissions("iot:project:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IotProject iotProject)
    {
        List<IotProject> list = iotProjectService.selectIotProjectList(iotProject);
        ExcelUtil<IotProject> util = new ExcelUtil<IotProject>(IotProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 新增项目管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目管理
     */
    @RequiresPermissions("iot:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IotProject iotProject)
    {
        iotProject.setProjectApikey(UUID.randomUUID().toString().replaceAll("-",""));
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        iotProject.setCreator(currentUser.getUserId().toString());
        iotProject.setCreateDate(new Date());
        return toAjax(iotProjectService.insertIotProject(iotProject));
    }

    /**
     * 修改项目管理
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") String projectId, ModelMap mmap)
    {
        IotProject iotProject = iotProjectService.selectIotProjectById(projectId);
        mmap.put("iotProject", iotProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目管理
     */
    @RequiresPermissions("iot:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IotProject iotProject)
    {
        SysUser currentUser = ShiroUtils.getSysUser();
        iotProject.setModifier(currentUser.getUserId().toString());
        iotProject.setModifyDate(new Date());
        return toAjax(iotProjectService.updateIotProject(iotProject));
    }

    /**
     * 删除项目管理
     */
    @RequiresPermissions("iot:project:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iotProjectService.deleteIotProjectByIds(ids));
    }
}
